import {reactive, readonly, watch, nextTick} from 'vue'
const hasCondition = (state, formId, needIndex = false) => {
    const index = state.processConditions.findIndex(d => d.formId === formId)
    return needIndex ? index : index > -1
}
// 判断审批人节点的表单数据是否已存在
const hasApprovers = (state, formId, needIndex = false) => {
    const index = state.processApprovers.findIndex(d => d.formId === formId)
    return needIndex ? index : index > -1
}
const hasStart = (state, formId, needIndex = false) => {
    const index = state.processStart.findIndex(d => d.formId === formId)
    return needIndex ? index : index > -1
}
// 创建一个响应式的状态对象
const state = reactive({
    processStart: [], // 开始节点
    processApprovers: [], // 传递流程图需要的审批人节点信息
    processConditions: [], // processConditions 用于传递流程图需要的条件
    formItemList: [] // 流程节点表单权限控制——组件列表
})
// 创建actions来修改状态
const actions = {
    addPStart(data) {
        if (data.formId === null || data.formId === undefined) return
        if (!hasStart(state, data.formId)) {
            state.processStart.unshift(data)
        }
    }, delPStart(formId) {
        if (formId === null || formId === undefined) return
        const index = hasApprovers(state, formId, true)
        const cons = state.processStart
        index > -1 && cons.splice(index, 1)
    }, clearPStart() {
        state.processStart = []
    }, initPStart(data) {
        state.processStart = data
    }, // 增加表单数据到审批人节点
    addPApprovers(data) {
        if (data.formId === null || data.formId === undefined) return
        if (!hasApprovers(state, data.formId)) {
            state.processApprovers.unshift(data)
        }
    }, // 删除表单数据到审批人节点
    delPApprovers(formId) {
        if (formId === null || formId === undefined) return
        const index = hasApprovers(state, formId, true)
        const cons = state.processApprovers
        index > -1 && cons.splice(index, 1)
    }, //  * 清除所有的审批人数据
    clearPApprovers() {
        state.processApprovers = []
    }, // 所有mutations中的方法的第一个参数一定是state变量，用来进行对state中的状态的操作
    // 第二个参数是可选参数，用于调用该 mutations 方法的时候传参
    initPConditions(data) {
        state.processConditions = data
    }, addPCondition(data) {
        if (data.formId === null || data.formId === undefined) return
        if (!hasCondition(state, data.formId)) {
            state.processConditions.unshift(data)
        }
    }, delPCondition(formId) {
        if (formId === null || formId === undefined) return
        const index = hasCondition(state, formId, true)
        const cons = state.processConditions
        //作为条件，删除该条件
        if (index > -1) {
            cons.splice(index, 1)
            return Promise.resolve(true) // 返回一个 resolved 的 Promise
        }
        return Promise.resolve(false) // 返回一个 rejected 的 Promise
    }, // 清除所有的条件
    clearPCondition() {
        state.processConditions = []
    }, updateFormItemList(list) {
        state.formItemList = list
    }, // 新增：执行操作后的nextTick回调
    async withNextTick(action) {
        await action()
        return nextTick()
    }
}
// 创建getters来派生状态
const getters = {}
// 创建一个函数来监听状态变化
const subscribe = (key, callback) => {
    watch(() => state[key], (newValue, oldValue) => {
        callback(newValue, oldValue)
    })
}
// 导出一个只读的状态，以及actions和getters
export default {
    state: readonly(state), ...actions, ...getters, subscribe, nextTick // 直接导出nextTick函数
}
